package com.study.talk.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.module.entity.Friend;
import com.study.talk.module.entity.InviteUserRecord;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.mapper.FriendMapper;
import com.study.talk.module.mapper.InviteUserRecordMapper;
import com.study.talk.module.mapper.UserInfoMapper;
import com.study.talk.module.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.talk.module.service.GroupInfoService;
import com.study.talk.module.service.GroupUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@Service
@Transactional
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {


    @Resource
    private FriendMapper friendMapper;

    @Resource
    private UserInfoMapper userInfoMapper;


    @Resource
    private InviteUserRecordMapper inviteUserRecordMapper;

    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Override
    public void addFriend(Integer userId, Integer friendId) {
        UserInfo friendInfo = userInfoMapper.selectById(friendId);
        Friend friend = new Friend(friendInfo, userId);
        friendMapper.insert(friend);
    }

    @Override
    public void removeFriend(Integer userId, Integer friendId) {
        friendMapper.removeFriend(userId,friendId);
    }

    @Override
    public void removeAllFriend(Integer userId) {
        friendMapper.removeAllFriend(userId);
    }

    @Override
    public boolean queryIsFriend(Integer userId, Integer friendId) {
        Friend friend = friendMapper.queryIsFriend(userId, friendId);
        return ObjectUtils.isNotNull(friend);
    }

    @Override
    public List<Friend> getFriendList(Integer userId) {
        return friendMapper.getFriendList(userId);
    }

    @Override
    public List<UserInfo> getUserInfoList(Integer userId) {
        return friendMapper.getUserInfoList(userId);
    }

    @Override
    public List<UserInfo> getUserInfoListNotInGroup(Integer groupId, Integer userId) {
        List<UserInfo> friendList = friendMapper.getUserInfoList(userId);
        List<UserInfo> newList = new ArrayList<>();
        for (UserInfo userInfo : friendList) {
            if (!groupUserInfoService.queryHasMember(groupId, userInfo.getId())){
                newList.add(userInfo);
            }
        }
        return newList;
    }

}
